import { inject, reactive, toRaw } from "vue";
import type * as PHStoreType from "@/store/modules/processHeader/type";
interface CallBackData {
  done(closed: boolean): void | boolean | Promise<void | boolean>;
  flag: boolean;
  status: string;
}
const useClick = (props: any) => {
  const provideState = inject("provideState", { getLesionItem: new Function() });
  const data = reactive({
    delVisible: false,
    item: {} as {
      lesionData: PHStoreType.LesionData;
      lesion: {
        signForm: PHStoreType.Sign;
        compList: PHStoreType.Component[];
      }
    }
  });
  // 删除弹窗事件
  const handleDelVisible = (logData: CallBackData) => {
    if (logData.status === 'save') {
      logData.done(true);
      data.delVisible = logData.flag;
      provideState.getLesionItem(() => {
        return {
          currentItem: {
            ...data.item
          },
          type: "del",
        };
      });
    } else {
      data.delVisible = logData.flag;
    };
  };
  const handleMenuClick = (item: Record<string, any>) => {
    let currentStatus = false;
    currentStatus = !item.isVisible;
    props.allData && props.allData.map((it: { isVisible: boolean; }) => (it.isVisible = false));
    item.isVisible = currentStatus;
  };
  const handleAllClick = (item: Record<string, any>) => {
    item.isAllVisible = !item.isAllVisible;
    item.viewList.map((it: { lesionData: { isApproveEye: boolean; }; }) => it.lesionData.isApproveEye = item.isAllVisible);
    provideState.getLesionItem(() => {
      return {
        currentItem: item,
        type: "allEyes",
      };
    });
  };
  const handleClick = (
    item: {
      lesionData: PHStoreType.LesionData;
      lesion: {
        signForm: PHStoreType.Sign;
        compList: PHStoreType.Component[];
      }
    },
    type: number
  ) => {
    if (type === 1) {
      let currentStatus = false;
      currentStatus = !item.lesionData.isCurrent;
      props.allData && props.allData.map((it: { viewList: any[]; }) => it.viewList.map((t: { lesionData: { isCurrent: boolean; }; }) => t.lesionData.isCurrent = false));
      item.lesionData.isCurrent = currentStatus;
      provideState.getLesionItem(() => {
        return {
          currentItem: {
            ...item
          },
          type: "lesion",
        };
      });
    } else {
      data.delVisible = true;
      data.item = item;
    };
  };
  const handleBtnClick = (
    item: {
      lesionData: PHStoreType.LesionData;
      lesion: {
        signForm: PHStoreType.Sign;
        compList: PHStoreType.Component[];
      }
    },
    type: number
  ) => {
    const tempArr = props.allData && props.allData.filter((it: { id: number; }) => it.id === item.lesionData.lesionType)[0];
    item.lesionData.isApproveEye = !item.lesionData.isApproveEye;
    const eyesStatusArr = tempArr.viewList && tempArr.viewList.map((it: { lesionData: { isApproveEye: boolean; }; }) => it.lesionData.isApproveEye);
    tempArr.isAllVisible = eyesStatusArr.includes(true);
    provideState.getLesionItem(() => {
      return {
        currentItem: {
          ...item
        },
        type: "eyes",
      };
    });
  };
  return {
    data,
    handleMenuClick,
    handleAllClick,
    handleClick,
    handleBtnClick,
    handleDelVisible
  }
};

export default useClick;